package com.richinfo.ajax.server.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.richinfo.ajax.server.model.JsonVO;

/**
 * Created by WangXJ
 * 2019-06-26 15:19
 */
@RequestMapping("/test")
@RestController
// 普通跨域(不允许cookie)
//@CrossOrigin
// 允许cookie的跨域
//@CrossOrigin(origins = "http://localhost:8081", allowCredentials = "true")
public class TestController {

	@GetMapping("/get1")
	public JsonVO get1(HttpServletResponse response) {

		// 简单请求的设置
//		response.addHeader("Access-Control-Allow-Origin", "*");
//		response.addHeader("Access-Control-Allow-Methods", "*");
		// 非简单请求的设置
//		response.addHeader("Access-Control-Allow-Headers", "Content-Type");
//		response.addHeader("Access-Control-Max-Age", "3600");// 让浏览器缓存信息的时长，单位秒
		
		return new JsonVO("get1 - ok");
	}
	
	@GetMapping("/get2")
	public JsonVO get2() {
		return new JsonVO("get2 - ok");
	}
	
	@GetMapping("/getCookie")
	public JsonVO getCookie(@CookieValue("cookie1")String cookie1) {
		
		return new JsonVO("getCookie" + cookie1);
	}
	
	@GetMapping("/getHeader")
	public JsonVO getHeader(@RequestHeader("x-header1")String header1, 
			@RequestHeader("x-header2")String header2) {
		
		return new JsonVO("getHeader" + header1 + " " + header2);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
